package org.example.env_boot.controller;

import org.example.env_boot.dto.ApiResponse;
import org.example.env_boot.entity.AqiFeedback;
import org.example.env_boot.entity.GridMember;
import org.example.env_boot.entity.Statistics;
import org.example.env_boot.service.AdminService;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 管理员控制器
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "*")
public class AdminController {
    
    private final AdminService adminService;
    
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }
    
    /**
     * 获取所有反馈信息
     */
    @GetMapping("/feedbacks")
    public ApiResponse<List<AqiFeedback>> getAllFeedbacks() {
        return adminService.getAllFeedbacks();
    }
    
    /**
     * 根据状态获取反馈信息
     */
    @GetMapping("/feedbacks/state/{state}")
    public ApiResponse<List<AqiFeedback>> getFeedbacksByState(@PathVariable Integer state) {
        return adminService.getFeedbacksByState(state);
    }
    
    /**
     * 分配反馈给网格员
     */
    @PostMapping("/feedbacks/{feedbackId}/assign")
    public ApiResponse<AqiFeedback> assignFeedback(@PathVariable Integer feedbackId,
                                                   @RequestBody AssignFeedbackRequest request) {
        return adminService.assignFeedbackToGridMember(feedbackId, Integer.parseInt(request.getGmId()));
    }
    
    /**
     * 获取系统统计数据
     */
    @GetMapping("/statistics/system")
    public ApiResponse<AdminService.SystemStatistics> getSystemStatistics() {
        return adminService.getSystemStatistics();
    }
    
    /**
     * 获取区域AQI统计
     */
    @GetMapping("/statistics/aqi-by-region")
    public ApiResponse<List<Object[]>> getAQIStatisticsByRegion() {
        return adminService.getAQIStatisticsByRegion();
    }
    
    /**
     * 获取月度统计数据
     */
    @GetMapping("/statistics/monthly")
    public ApiResponse<List<Object[]>> getMonthlyStatistics() {
        return adminService.getMonthlyStatistics();
    }
    
    /**
     * 获取网格员工作量统计
     */
    @GetMapping("/statistics/grid-member-workload")
    public ApiResponse<Map<String, Object>> getGridMemberWorkload() {
        return adminService.getGridMemberWorkload();
    }
    
    /**
     * 根据地区查询统计数据
     */
    @GetMapping("/statistics/by-region")
    public ApiResponse<List<Statistics>> getStatisticsByRegion(@RequestParam(required = false) Integer provinceId,
                                                               @RequestParam(required = false) Integer cityId) {
        return adminService.getStatisticsByRegion(provinceId, cityId);
    }
    
    /**
     * 根据日期范围查询统计数据
     */
    @GetMapping("/statistics/by-date-range")
    public ApiResponse<List<Statistics>> getStatisticsByDateRange(@RequestParam String startDate,
                                                                  @RequestParam String endDate) {
        return adminService.getStatisticsByDateRange(startDate, endDate);
    }
    
    /**
     * 获取可分配的网格员列表
     */
    @GetMapping("/grid-members/available")
    public ApiResponse<List<GridMember>> getAvailableGridMembers(@RequestParam(required = false) Integer provinceId,
                                                                 @RequestParam(required = false) Integer cityId) {
        return adminService.getAvailableGridMembers(provinceId, cityId);
    }
    
    /**
     * 获取待分配的反馈信息（状态为0）
     */
    @GetMapping("/feedbacks/pending")
    public ApiResponse<List<AqiFeedback>> getPendingFeedbacks() {
        return adminService.getFeedbacksByState(0);
    }
    
    /**
     * 获取已分配的反馈信息（状态为1）
     */
    @GetMapping("/feedbacks/assigned")
    public ApiResponse<List<AqiFeedback>> getAssignedFeedbacks() {
        return adminService.getFeedbacksByState(1);
    }
    
    /**
     * 获取已确认的反馈信息（状态为2）
     */
    @GetMapping("/feedbacks/confirmed")
    public ApiResponse<List<AqiFeedback>> getConfirmedFeedbacks() {
        return adminService.getFeedbacksByState(2);
    }
    
    // 请求类定义
    public static class AssignFeedbackRequest {
        @NotBlank(message = "网格员ID不能为空")
        private String gmId;
        
        public String getGmId() { return gmId; }
        public void setGmId(String gmId) { this.gmId = gmId; }
    }
} 